package com.futong.cloud.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.futong.cloud.action.ActionContext;
import com.futong.cloud.action.ActionModel;
import com.huaweicloud.sdk.ecs.v2.model.CreateServersRequest;
import lombok.Data;

import java.util.List;

public class JsonUtils {
    public static String evalStr(Object obj, String filedPath){
        Object eval = JSONPath.eval(obj, filedPath);
        if(eval.getClass().isPrimitive()){
            return eval+"";
        }else if(eval instanceof String){
            return (String) eval;
        }else{
            return JSON.toJSONString(eval);
        }
    }
    public static Object eval(Object obj, String filedPath){
        return JSONPath.eval(obj, filedPath);
    }
    public static void main(String[] args) {
        String json=("{\"name\":\"z3\",\"data\":[{\"score\":\"12\",\"subject\":\"数学\",\"year\":\"2021\"},{\"score\":\"13\",\"subject\":\"语文\"}]}");
        JSONObject jsonObject=JSON.parseObject(json);
        Object read = JSONPath.eval(jsonObject, "$.data[0]['year','subject']");
        System.out.println(read);
//        JSONObject request=JSON.parseObject("{\n" +
//                "\"action\":\"listZones\",\n" +
//                "\"clientId\":\"bbbbbb\",\n" +
//                "\"regionId\":\"cn-east-2\"\n" +
//                "}");
//        ActionContext context=new ActionContext(request,new ActionModel());
//        Object eval = JSONPath.eval(context, "$.request.clientId");
//        System.out.println(eval);
        CreateServersRequest request=new CreateServersRequest();
        JSONPath.set(request,"$.body.server.name","test");
        System.out.println(request);
//        JSONObject jsonObject1=new JSONObject();
//        jsonObject1.put("name","北京大学");
//        jsonObject1.put("path","朝阳");
//        JSONPath.set(jsonObject,"$.school",jsonObject1);
//        System.out.println(jsonObject);
//        Person person = new Person();
//        JSONPath.set(person,"$.name","张三");
//        JSONPath.set(person,"$.aihao",new ArrayList<>());
//        JSONPath.arrayAdd(person,"$.aihao","唱歌","跳舞");
//        Object eval = JSONPath.eval(person, "$.aihao[1]");
//        System.out.println(eval);
//        System.out.println(person);
    }
    @Data
    static class Person{
        String name;
        String age;
        String sex;
        List<String> aihao;
    }
}
